//
//  PGTopicTableViewController.m
//  PhotographicGuide
//
//  Created by indream on 13-4-12.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGTopicTableViewController.h"
#import "CKRefresh/CKRefreshControl.h"
#import "PGTopicViewController.h"
#import "PGTopicManager.h"
#import "PGArticleManager.h"
#import "PGWebViewController.h"
static PGTopicTableViewController*_instance;

static NSString* NOTIFICATION_TOPIC_TABLE_REFRESH = @"__NOTIFICATION_TOPIC_TABLE_REFRESH__";

@interface PGTopicTableViewController ()

@end

@implementation PGTopicTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:[PGTopicTableViewController sharedController] action:@selector(doRefresh:) forControlEvents:UIControlEventValueChanged];
    [self doRefresh:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTable:) name:NOTIFICATION_TOPIC_TABLE_REFRESH object:nil];
    self.tableView.backgroundColor = [UIColor blackColor];
}
-(void)viewDidUnload{
    [self setTvCell:nil];
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:NOTIFICATION_TOPIC_TABLE_REFRESH object:nil];
}
- (void)doRefresh:(CKRefreshControl *)sender {
    [[PGTopicManager sharedManager]requestList];
}
-(void)reloadTable:(NSNotification*)notification{
    [self.refreshControl endRefreshing];
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [PGTopicManager sharedManager].topicList.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        return 100.0;
    }else{
        return 240.0;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PGTopicCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell==nil){
        [[NSBundle mainBundle] loadNibNamed:@"PGTopicCell" owner:self options:nil];
        cell = self.tvCell;
        self.tvCell = nil;
    }
    cell.titleLabel.text = [[[PGTopicManager sharedManager].topicList objectAtIndex:indexPath.row] objectForKey:@"title"];
    [cell loadImage:[[[PGTopicManager sharedManager].topicList objectAtIndex:indexPath.row] objectForKey:@"image"]];
    [cell updateLabelSize:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString*webURL=[[[PGTopicManager sharedManager].topicList objectAtIndex:indexPath.row] objectForKey:@"url"];
    if([webURL isEqualToString:@""]){
        PGTopicViewController *controller = [[PGTopicViewController alloc]initWithNibName:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?@"PGTopicViewController":@"PGTopicViewController_iPad" bundle:nil];
        controller.title = [[[PGTopicManager sharedManager].topicList objectAtIndex:indexPath.row] objectForKey:@"title"];
        controller.tag = [[[PGTopicManager sharedManager].topicList objectAtIndex:indexPath.row] objectForKey:@"tag"];
        [self.navigationController pushViewController:controller animated:YES];
    }else{
        if([webURL componentsSeparatedByString:@"http://"].count>1){
            PGWebViewController *controller = [[PGWebViewController alloc]init];
            controller.url = webURL;
            controller.title = [[[PGTopicManager sharedManager].topicList objectAtIndex:indexPath.row] objectForKey:@"title"];
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
    
}
-(void)doFinishLoadMore{
    [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
}
+(PGTopicTableViewController*)sharedController{
    if(_instance==nil){
        _instance = [[PGTopicTableViewController alloc]init];
    }
    return _instance;
}

@end
